<?php

// 获取活动列表
// GET activity.volist

namespace app\api\resource\activity;
use think\Db;
use think\Session;
use app\api\lib\Page;

class Volist
{
    // 方法
    public $_method = ['GET'];
    // 前置
    public $_pre    = [];
    // 描述
    public $_description = '获取活动列表';
    // 参数
    public $_param  = [
        'model' => '分类,线上|线下',
        'city'  => '城市',
    ];
    // 返回
    public $_return = [
        200 => '获取成功',
    ];

    public function run(&$request)
    {
        $page  = $request->get('page/d',1);
        $show  = $request->get('show/d',10);
        $model = $request->get('model/s','');
        $city  = $request->get('city/s','');

        $table = 'activity';
        if ( !empty($model) ) {
            if ('线上' == $model) {
                $where['localtion'] = '线上';
            } elseif ('线下' == $model) {
                $where['localtion'] = empty($city) ? ['neq','线上'] : $city;
            }
        } else {
            if ( !empty($city) ) $where['localtion'] = $city;
        }
        $where['start_time'] = ['<',time()];
        $where['end_time']   = ['>',time()];
        $list = Db::name($table)->where($where)->order('id desc')->page($page,$show)->select();

        if ( !empty($list) ) {
            foreach ($list as $key => $val) {
                if ( Session::has('member') ) {
                    // 是否报名
                    if ( !empty($list) ) {
                        $memberId = Session::get('member.id');
                        foreach ($list as $key => $val) {
                            $ret = Db::name('activity_signup')->where('activity_id',$val['id'])->where('member_id',$memberId)->find();
                            $list[$key]['is_sign_up'] = is_null($ret) ? 0 : 1;
                        }
                    }
                }
                // 报名统计
                if ( !empty($list) ) {
                    foreach ($list as $key => $val) {
                        $list[$key]['sign_up_count'] = Db::name('activity_signup')->where('activity_id',$val['id'])->count();
                    }
                }
            }
        }

        $pagelist = Page::run($table, $page, $show, $where, $list);

        return [200,$pagelist];
    }
}